using System.Collections.Generic;
using HybridCLR;
using Panty.QF;
using System.Threading.Tasks;
using UnityEngine;
namespace Panty.Test
{
    public class HotfixGameStart : MonoBehaviour, IPermissionProvider
    {       
        IModuleHub IPermissionProvider.Hub => GameInitHub.GetIns();
        private void Awake()
        {
            DontDestroyOnLoad(this);
        }

        private async void Start()
        {
#if !UNITY_EDITOR
            //加载热更新DLL逻辑
            await LoadMetadataForAOTAssemblies();
            //HubTool.Initialize();
            StartGames();         
#endif
#if UNITY_EDITOR
            //编辑器下可以直接运行
            StartGames();
#endif

        }


        async Task LoadMetadataForAOTAssemblies()
        {
            List<string> aotMetaAssemblyFiles = new List<string>()
            {
                "mscorlib.dll",
                "System.dll",
                "System.Core.dll",
                //"UniTask.dll",
                "DOTweenPro.Scripts.dll",
                "DOTween.Modules.dll",
            };
            HomologousImageMode mode = HomologousImageMode.SuperSet;
            foreach (var aotDllName in aotMetaAssemblyFiles)
            {
                var package = await this.GetModel<IResLoader>().AsyncLoad<TextAsset>($"DefaultPackage:{aotDllName}");
                byte[] dllBytes = package.bytes;
                LoadImageErrorCode err = RuntimeApi.LoadMetadataForAOTAssembly(dllBytes, mode);
                Debug.Log($"LoadMetadataForAOTAssembly:{aotDllName}. mode:{mode} ret:{err}");
            }
        }
       public async void StartGames()
        {
            //加载UI示例 设置开场加载过渡视频动画
            //this.SendCmdP<ICmdInitMap, string>($"{1001}");
            this.EnumEvent<E_UICanvasEvt, (int tall, int curretn, long tallbytes, long currentbytes, bool isplayvideo, bool isassetupdata)>(E_UICanvasEvt.过渡动画开始, (0, 0, 0, 0, true, false));

            var curs = await this.GetModel<IResLoader>().AsyncLoad<GameObject>("DefaultPackage:Cursour");
            curs.InstantiateWithParent(this);
            await this.GetModel<IUIModule>().LoadUI("UICanvas_大背景");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_公告");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_选择区服");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_登录注册");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_选择角色");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_创建角色");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_提示");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_主界面");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_角色属性");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_背包");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_召唤兽");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_队伍栏");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_宠物");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_系统设置");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_大地图");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_任务栏");
            await this.GetModel<IUIModule>().LoadUI("UICanvas_查看召唤兽");
            UtilsController.Instance.LoadData();
            this.EnumEvent(E_UICanvasEvt.过渡动画结束);
        }

    }


    public class GameInitUI : UIPanel, IPermissionProvider
    {
        IModuleHub IPermissionProvider.Hub => GameInitHub.GetIns();
    }
}